import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.evalRPN(new String[]{"2","1","+","3","*"}));
        System.out.println(s.evalRPN(new String[]{"4","13","5","/","+"}));
        System.out.println(s.evalRPN(new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"}));
    }
}


class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        List<String> opts = Arrays.asList("+", "-", "*", "/");
        for (String token : tokens) {
            if (opts.contains(token)) {
                int operandR = stack.pop();
                int operandL = stack.pop();
                stack.push(calc(operandL, operandR, token));
            } else {
                stack.push(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }

    int calc(int operandL, int operandR, String opt) {
        switch (opt) {
            case "+":
                return operandL + operandR;
            case "-":
                return operandL - operandR;
            case "*":
                return operandL * operandR;
            case "/":
                return operandL / operandR;
            default:
                return 0;
        }
    }
}